package com.luck.emp.check.web;


import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.util.WebUtils;

import com.luck.emp.check.logic.ICheckLogic;
import com.luck.emp.check.qry.CheckQry;
import com.luck.emp.check.vo.CheckDetailExcel;
import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.entity.EmpTWhCheckD;
import com.luck.emp.entity.EmpTWhCheckM;
import com.luck.emp.product.logic.IProductLogic;
import com.luck.emp.report.logic.IReportLogic;
import com.luck.emp.report.vo.StorageVo;
import com.luck.emp.wh.logic.IWhLogic;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class CheckAction extends BaseActionSupport {
		/**
		 * 逻辑处理类
		 */
		ICheckLogic checkLogic;
		IWhLogic whLogic;
		IProductLogic productLogic;
		IReportLogic reportLogic;
		/**
		 * 查询对象
		 */
		CheckQry qry;
		/**
		 * 分页对象
		 **/
		Page page;
		/**
		 * 实体类
		 */
		EmpTWhCheckM entity;
		String[] productCodes,planQtys,confirmQtys,notes;
		List<EmpTWarehouse> whList;
		List<StorageVo> vos;
		List<EmpTWhCheckD> checkds;
		
		/**
		 * 列表
		 */
		public String list() throws Exception{
			UserBean userInfo = super.getUserInfo();
			whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (CheckQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new CheckQry();
			}
			qry.setUserInfo(userInfo);
			page = checkLogic.findCheckPage(qry, Page.class);
			return "list";
		}
		/**
		 * 新增
		 **/
		public String toAdd() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = new EmpTWhCheckM();
			entity.setCreateUser(userInfo.getUser());
			entity.setCreateDate(DateUtil.getDate());
			whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
			//EmpTWarehouse wh = reportLogic.get(EmpTWarehouse.class, qry.getWhCode());
			//entity.setWh(wh);
			/*ReportQry reportQry  = new ReportQry();
			reportQry.setWhCode(qry.getWhCode());
			reportQry.setSearchProductType("1");
			reportQry.setUserInfo(userInfo);
			vos = reportLogic.findStorage(reportQry);*/
			super.event="ADD";
			super.title="新增库存盘点";
			return "edit";
		}
		/**
		 * 保存盘点单
		 **/
		public String edit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			//新增操作
			entity.setCheckCode(null);
			checkLogic.saveCheck(userInfo,entity,productCodes,planQtys);
			super.redirectUrl = "/emp/check_list.action";
			return super.GLOBAL_SUCCESS;
		}
		//反馈盘点单
		public String toFeedback() throws Exception{
			entity = checkLogic.get(EmpTWhCheckM.class,entity.getCheckCode());
			if(entity.getStatus().equals("1") || entity.getStatus().equals("2"))
				checkds = checkLogic.findChecksByCheckCode(entity.getCheckCode());
			else{
				super.msgStr = "只有新建或已反馈的单据才能反馈！";
				return GLOBAL_ERROR;
			}
			super.title="反馈盘点单";
			return "feedback";
		}
		//保存反馈盘点单内容
		public String feedback() throws Exception{
			UserBean userInfo = super.getUserInfo();
			//得到盘点单明细id
			String[] detailIds = super.getRequest().getParameterValues("detailIds");
			String str = "";
			try {
				str = checkLogic.saveCheckFeedback(userInfo,entity,detailIds,confirmQtys,notes);
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				super.msgStr = str;
				return super.GLOBAL_ERROR;
			}
			super.redirectUrl = "/emp/check_list.action";
			return super.GLOBAL_SUCCESS;
		}
		
		
		/**
		 * 删除
		 **/
		public String delete() throws Exception{			
			UserBean userInfo = super.getUserInfo();
			entity = checkLogic.get(EmpTWhCheckM.class,entity.getCheckCode());
			String str = "";
			try {
				str = this.checkLogic.deleteCheck(userInfo,entity);
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				this.msgStr = str;
				super.renderJsonError(str);
			}else
				super.renderJsonSuccess("删除成功!");
			return null;
			
		}

		public String auditList() throws Exception{
			UserBean userInfo = super.getUserInfo();
			whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (CheckQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new CheckQry();
			}
			qry.setUserInfo(userInfo);
			page = checkLogic.findCheckPage(qry, Page.class);
			return "auditList";
		}
		public String toAudit() throws Exception{
			entity = checkLogic.get(EmpTWhCheckM.class, entity.getCheckCode());
			checkds = checkLogic.findChecksByCheckCode(entity.getCheckCode());
			if(!entity.getStatus().equals("2")){
				super.msgStr = "已反馈的盘点单才能审核";
				return super.GLOBAL_ERROR;
			}
			super.event="AUDIT";
			super.title="库存盘点审核";
			return "audit";
		}
		public String audit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			EmpTWhCheckM m = checkLogic.get(EmpTWhCheckM.class, entity.getCheckCode());
			if(!m.getStatus().equals("2")){
				super.msgStr = "已反馈的盘点单才能审核";
				return super.GLOBAL_ERROR;
			}
			checkLogic.saveAudit(userInfo,entity);
			super.redirectUrl = "/emp/check_auditList.action";
			return super.GLOBAL_SUCCESS;
		}
		/**
		 * 修改
		 **/
		public String view() throws Exception{
			entity = checkLogic.get(EmpTWhCheckM.class,entity.getCheckCode());
			checkds = checkLogic.findChecksByCheckCode(entity.getCheckCode());
			super.title="库存盘点详细信息";
			return "view";
		}
		public String print() throws Exception{
			entity = checkLogic.get(EmpTWhCheckM.class,entity.getCheckCode());
			checkds = checkLogic.findChecksByCheckCode(entity.getCheckCode());
			super.title="库存盘点详情";
			return "print";
		}
		/**
		 * 提供弹出窗口选择商品，带库存数
		 * @return
		 * @throws Exception
		 */
		public String selectProduct() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (CheckQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new CheckQry();
			}
			EmpTWarehouse wh = checkLogic.get(EmpTWarehouse.class, qry.getWhCode());
			qry.setWhName(wh.getWhName());
			qry.setUserInfo(userInfo);
			qry.setStatus("1");
			page = checkLogic.findCheckProductPage(qry, Page.class);
			return "selectProduct";
		}

		public String selectSl() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (CheckQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new CheckQry();
			}
			qry.setUserInfo(userInfo);
			qry.setStatus("1");
			page = checkLogic.findCheckProductPage(qry, Page.class);
			return "selectSl";
		}
		/**
		 * 根据传入的商品条码和机构ID来得到商品对象
		 * @return
		 * @throws Exception
		 */
		public String selectProductByBar() throws Exception{
			UserBean userInfo = super.getUserInfo();
			qry.setOperateType("barcode");
			StorageVo vo = new StorageVo();
			qry.setUserInfo(userInfo);
			List<StorageVo> vos = checkLogic.findCheckProductPage(qry, List.class);
			if(vos != null && !vos.isEmpty())
				vo = vos.get(0);
			Map<String, String> jsonMap = new HashMap<String, String>();
			jsonMap.put("status", SUCCESS);
			jsonMap.put("productCode", vo.getProductCode());
			jsonMap.put("productName", vo.getProductName());
			jsonMap.put("measUnit", vo.getMeasUnit());
			jsonMap.put("storeQty", vo.getCurrentQry()+"");
			jsonMap.put("price", vo.getPriceStr());
			jsonMap.put("lowPrice", vo.getLowPriceStr());
			jsonMap.put("message", "操作成功");
			JSONObject jsonObject = JSONObject.fromObject(jsonMap);
			this.rendText(jsonObject.toString());
			return null;
		}
		public String selectDb() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (CheckQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new CheckQry();
			}
			EmpTWarehouse wh = checkLogic.get(EmpTWarehouse.class, qry.getWhCode());
			qry.setWhName(wh.getWhName());
			qry.setUserInfo(userInfo);
			page = checkLogic.findCheckProductPage(qry, Page.class);
			return "selectDb";
		}
		public String exportCheck() throws Exception {
			entity = checkLogic.get(EmpTWhCheckM.class,entity.getCheckCode());
			checkds = checkLogic.findChecksByCheckCode(entity.getCheckCode());
			StringBuffer xlsFile = new StringBuffer();
			xlsFile.append(WebUtils.getRealPath(this.getRequest().getSession().getServletContext(), "/"));
			xlsFile.append("template/emp/check/checkdetail.xls");

			StringBuffer sheetName = new StringBuffer();
			sheetName.append("盘点单");
			CheckDetailExcel excel = new CheckDetailExcel();
			excel.setCheck(entity);
			excel.setFromIdx(3);
			excel.build(new FileInputStream(xlsFile.toString()), checkds,sheetName.toString());

			getResponse().setContentType(CheckDetailExcel.getExcelContentType());
			getResponse().setHeader(
					"Content-Disposition",
					"filename=\""
							+ new String(("盘点单["+entity.getCheckCode()+"]"+".xls").getBytes("gb2312"),"ISO8859-1") + "\";");
			ServletOutputStream out = this.getResponse().getOutputStream();
			excel.getWorkbook().write(out);
			out.flush();
			out.close();

			return null;
		}
		public CheckQry getQry(){
			return this.qry;
		}
		public void setQry(CheckQry qry){
			this.qry = qry;
		}
		
		public ICheckLogic getCheckLogic(){
			return this.checkLogic;
		}
		public void setCheckLogic(ICheckLogic checkLogic){
			this.checkLogic = checkLogic;
		}
		public Page getPage() {
			return this.page;
		}
		public void setPage(Page page) {
			this.page = page;
		}
		public EmpTWhCheckM getEntity(){
			return entity;
		}
		public void setEntity(EmpTWhCheckM entity){
			this.entity = entity;
		}
		public IWhLogic getWhLogic() {
			return whLogic;
		}
		public void setWhLogic(IWhLogic whLogic) {
			this.whLogic = whLogic;
		}
		public IProductLogic getProductLogic() {
			return productLogic;
		}
		public void setProductLogic(IProductLogic productLogic) {
			this.productLogic = productLogic;
		}
		public String[] getProductCodes() {
			return productCodes;
		}
		public void setProductCodes(String[] productCodes) {
			this.productCodes = productCodes;
		}
		public String[] getPlanQtys() {
			return planQtys;
		}
		public void setPlanQtys(String[] planQtys) {
			this.planQtys = planQtys;
		}
		public String[] getConfirmQtys() {
			return confirmQtys;
		}
		public void setConfirmQtys(String[] confirmQtys) {
			this.confirmQtys = confirmQtys;
		}
		public String[] getNotes() {
			return notes;
		}
		public void setNotes(String[] notes) {
			this.notes = notes;
		}
		public List<EmpTWarehouse> getWhList() {
			return whList;
		}
		public void setWhList(List<EmpTWarehouse> whList) {
			this.whList = whList;
		}
		public IReportLogic getReportLogic() {
			return reportLogic;
		}
		public void setReportLogic(IReportLogic reportLogic) {
			this.reportLogic = reportLogic;
		}
		public List<StorageVo> getVos() {
			return vos;
		}
		public void setVos(List<StorageVo> vos) {
			this.vos = vos;
		}
		public List<EmpTWhCheckD> getCheckds() {
			return checkds;
		}
		public void setCheckds(List<EmpTWhCheckD> checkds) {
			this.checkds = checkds;
		}

		
}